package jp.sourceforge.akjrcp.ui.param;

import java.util.ArrayList;
import java.util.List;

import jp.sourceforge.akjrcp.ui.param.AbstractParamCompositeItem.SelectionCheck;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;

public class ParamRadioComposite extends AbstractParamCompositeItem{
private String key;

private String[] paramsValues;
private String[] paramLabels;
private String value="";//setted value
List<Button> buttons=new ArrayList<Button>();

private Composite composite;
public String getKey() {
	return key;
}



public void setToolTipText(String text){
	composite.setToolTipText(text);
	for (int i = 0; i < buttons.size(); i++) {
		buttons.get(i).setToolTipText(text);
	}
}

public String[] getParams() {
	return paramsValues;
}

public String getValue(){
	String ret="";
	for (int i = 0; i < buttons.size(); i++) {
		Button bt=buttons.get(i);
		if(bt.getSelection()){
			return paramsValues[i];
		}
	}
	return ret;
}

public void setValue(String value) {
	
	int index=-1;
	for (int i = 0; i < paramsValues.length; i++) {
		if(paramsValues[i].equals(value)){
			index=i;
			break;
		}
	}
	//log.infoparam radio set "+value +" index="+index);
	if(index!=-1){
		String selectLabel=paramLabels[index];
		for (int i = 0; i < buttons.size(); i++) {
			Button bt=buttons.get(i);
				if(bt.getText().equals(selectLabel)){
					bt.setSelection(true);
					this.value = value;
				}else{
					bt.setSelection(false);
				}
		}
	}else{
		//ignore set value
	}
}
public ParamRadioComposite(Composite parent, int style,String key,String[] values,String[] labels ){
	this(parent,style,key,values,labels,1);
}
	public ParamRadioComposite(Composite parent, int style,String key,String[] values,String[] labels,int grid) {
		super(parent, style);
		this.key=key;
		this.paramsValues=values;
		this.paramLabels=labels;
		this.setLayout(new FillLayout());
		composite = new Composite(this,SWT.NONE);
		
		
		
		composite.setLayout(new GridLayout(grid,false));
		SelectionCheck ch=new SelectionCheck();
		for (int i = 0; i < labels.length; i++) {
			Button radio=new Button(composite,SWT.RADIO);
			if(i==0){
				radio.setSelection(true);//default index 0 selected
			}
			radio.addSelectionListener(ch);
			radio.setText(labels[i]);
			buttons.add(radio);
		}
		
	}


	
	public boolean isModified() {
		String newValue=getValue();
		return !value.equals(newValue);
	}

}
